Skip to content

fix(android): prevent crash when Promise.reject is called with null error code#1325

Open
ivannikcevicc wants to merge 1 commit intodotintent:masterfrom
ivannikcevicc:master
Open

fix(android): prevent crash when Promise.reject is called with null error code#1325
ivannikcevicc wants to merge 1 commit intodotintent:masterfrom
ivannikcevicc:master

Conversation

@ivannikcevicc
Copy link

Fix: Prevent Android crash when BLE disconnect error has null error code

Summary

This PR fixes a fatal Android crash that occurs when Promise.reject() is called with a null error code during BLE disconnect flows.

On Android, RxAndroidBle emits a BleDisconnectedException for both expected and unexpected disconnects. In some cases, the propagated error does not contain a non-null error code, which violates React Native's Promise.reject contract and results in a NullPointerException.

This change ensures a safe fallback error code is always provided.


Problem

During normal BLE lifecycle events (navigation cleanup, explicit disconnect, device power loss), the app may crash with:

java.lang.NullPointerException:
Parameter specified as non-null is null:
method com.facebook.react.bridge.PromiseImpl.reject, parameter code

This crash occurs even when the disconnect is expected and properly handled in JavaScript.


Root Cause

SafePromise.reject(...) forwards a nullable code value into Promise.reject(code, ...).

React Native requires code to be non-null, which causes a runtime crash.


Solution

Provide a defensive fallback error code when code is null before calling Promise.reject.

Example:

String safeCode = code != null ? code : "UNKNOWN_ERROR";
promise.reject(safeCode, message, throwable);

Impact

  • Prevents Android crashes during normal BLE disconnect flows
  • No behavior change for JS consumers
  • Improves stability in real-world BLE lifecycle scenarios

Previously, SafePromise.reject would call React Native's Promise.reject with a null 'code' parameter, causing a NullPointerException on Android.

This commit ensures that all reject methods provide a non-null fallback code ('UNKNOWN_ERROR') and message if any argument is null, preventing crashes when BLE operations fail without an explicit error code.

Also preserves atomic isFinished logic to prevent multiple resolve/reject calls.
@theburke9
Copy link

run into this issue and implemented this fix, now everything works correctly 👍

@ivannikcevicc
Copy link
Author

@theburke9 Glad to hear, I hope it gets implemented. Some people tried to fix the issue by changing the way SafePromise is called in some instances, yet I'm the only one whose issue actually fixes the SafePromise class itself. But unfortunately, It seems this repo is poorly maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants